#!/usr/bin/env python
# -*- coding: utf8 -*-
from logfacade import LoggerFactory


'''
Created on 2011-10-9

@author: zhongfeng
'''

from crawlerhttp import crawle
import re
from threadpool import ThreadPool, WorkRequest
from dbproc.basedbproc import getConnect,MySQLQueryPagination

def getProdImgUrlFromProdId(*prodId):
    rawId,id = prodId[0]
    baseUrl = r'http://www.360buy.com/lishiset.aspx?callback=jdRecent.setData&id=%s'
    url = baseUrl % rawId
    result = crawle(url)
    imgUrl = ''
    if result.code == 200:
        ct = re.compile(r'{.*}').search(result.content)
        if ct != None:
            jd = eval(ct.group())
            imgUrl = jd['img']
    imgUrl = imgUrl.replace('n5', 'n2',1)
    return id,imgUrl

def proc_result(request, result):
    logger = LoggerFactory.getLogger(logName='360buy')
    logger.info(result)

def main_crawle(prodIdList,thread_num = 20):
    try:
        pool = ThreadPool(thread_num)     
        for prodId in prodIdList:
            req = WorkRequest(getProdImgUrlFromProdId, [prodId], None,
                        callback=proc_result)      
            pool.putRequest(req)
        pool.wait()
    except Exception,e:
            raise e
    finally:
            pool.dismissWorkers(num_workers=thread_num)

if __name__ == '__main__':
    conn = getConnect()
    queryPagin = MySQLQueryPagination(conn,numPerPage = 1000)
    sql = 'SELECT raw_id,id FROM `prod_base_info_3c` where site_id = 6'
    for prodIds in queryPagin.queryForList(sql):
        main_crawle(prodIds)
    conn.close()
    
    